package me.kany.project.spring.boot.listener;

import ch.qos.logback.classic.spi.LoggingEventVO;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created by Jason.Wang on 2016-07-18.
 */
@Component
public class LogMessageListener implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(LogMessageListener.class.getCanonicalName());

    public void onMessage(Message message) {
        try {
            // receive log event in your consumer
            LoggingEventVO event = (LoggingEventVO) ((ActiveMQObjectMessage) message).getObject();
            System.out.println("Received log [" + event.getLevel() + "]: " + event.getMessage());
            logger.info(event.getLevel() + event.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
